package com.rfid.afd.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.afd.activity.adapter.GatherAdapter;
import com.rfid.afd.activity.view.IGatherView;
import com.rfid.afd.presenter.BasePresenter;
import com.rfid.afd.presenter.GatherPresenter;
import com.rfid.db.bean.Rfid;
import com.rfid.rls_psw.R;
import com.rfid.util.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**后台数据库内无书籍信息，但是图书馆数据库有，采集相关书籍EPC给后台，后台去对方数据库拿去数据补充后台数据*/
public class GatherActivity extends BaseActivity<GatherPresenter> implements IGatherView {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.rfid)
    TextView mShelfTextView;
    @BindView(R.id.check)
    TextView mCheck;
    @BindView(R.id.back)
    Button mBack;
    private GatherAdapter mAdapter;
    private List<Epc> mData;
    boolean isStarted;

    @BindView(R.id.edit)
    EditText mEditText;
    @BindView(R.id.edit_confirm)
    Button mEditConfirm;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gather_layout);
        ButterKnife.bind(this);
        mData = new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        mAdapter = new GatherAdapter(this,mData);
        mRecyclerView.setAdapter(mAdapter);
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mRecyclerView.setLayoutAnimation(controller);
        mShelfTextView.setText("请扫描层标");
        mEditText.setVisibility(View.GONE);
    }

    @Override
    public GatherPresenter getPresenter() {
        return new GatherPresenter();
    }

    @Override
    public void onScaned(String epc, String barcode) {
        Epc epc1 = new Epc(epc,barcode);
        LogUtils.v("onScaned mData.contains(epc1) ? " + (mData.contains(epc1)));
        if(!mData.contains(epc1)) {
            mData.add(epc1);
            LogUtils.v("notifyDataSetChanged " + mData.size());
            mCheck.setText("" + mData.size());
            mAdapter.notifyDataSetChanged();
        }
    }

    public void start(View v) {
        scan((TextView) v,!isStarted);
    }

    private void scan(TextView v, boolean b) {
        TextView button = v;
        if(!b) {
            button.setText("开始");
            mPresenter.stopScanRfid();
            mBack.setEnabled(true);
            mEditConfirm.setEnabled(true);
        } else {
            button.setText("停止");
            mPresenter.startScanRfid();
            mBack.setEnabled(false);
            mEditConfirm.setEnabled(false);
        }
        isStarted = b;
    }

    @OnClick(R.id.edit_confirm)
    public void onClick(View view) {
        if(view == mEditConfirm) {
            boolean v = mEditText.getVisibility() != View.VISIBLE;
            switchEdit(v);
        }
    }

    private void switchEdit(boolean v) {
        if(!v) {
            mEditText.setVisibility(View.GONE);
            mShelfTextView.setVisibility(View.VISIBLE);
            mEditConfirm.setText("编辑");
            String shelf = mEditText.getText().toString();
            if(!TextUtils.isEmpty(shelf)) {
                String os = mShelfTextView.getText().toString();
                System.out.println("os = " + os + ",shelf = " + shelf);
                if(!shelf.equals(os))
                    onShelf(shelf);
            } else {
                mShelfTextView.setText("请扫描层标");
            }
        } else {
            mEditConfirm.setText("保存");
            mShelfTextView.setVisibility(View.GONE);
            mEditText.setVisibility(View.VISIBLE);
        }
    }

    public void back(View view) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        if(isStarted) {
            return;
        }
        super.onBackPressed();
    }

    public void complete(View v) {
        scan(findViewById(R.id.scan),false);
        mPresenter.gather(mData);
        mData.clear();
        mShelfTextView.setText("已采集，请扫描层标");
        mEditText.setText("");
        switchEdit(false);
        mCheck.setText("");
        mAdapter.notifyDataSetChanged();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mPresenter.setDisableShelf(false);
    }

    @Override
    public void onShelf(String shelf) {
        mPresenter.setDisableShelf(true);
        mShelfTextView.setText(shelf);
        mEditText.setText(shelf);
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void stopLoading() {
    }

    public class Epc {
        public String epc;
        public String barcode;
        public Epc(String epc,String barcode) {
            this.epc = epc;
            this.barcode = barcode;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Epc epc1 = (Epc) o;
            return Objects.equals(epc, epc1.epc) &&
                    Objects.equals(barcode, epc1.barcode);
        }

        @Override
        public int hashCode() {
            return Objects.hash(epc, barcode);
        }
    }
}
